﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using QuanLyKhachSan;
using System.Data;
using System.Data.SqlClient;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.DAO;

namespace UnitTest
{
    public  class clsTestKhachHang:clsTest 
    {
        public KhachHangDTO khachHangdto;

        public bool Assert_clauses(DataTable expected, KhachHangDTO actual)
        {
            if (expected.Rows.Count > 0)
            {
                DataRow dr= expected.Rows[0];
                Assert.AreEqual((int)dr["maKhachHang"], actual.MaKhachHang, "I hope the maKhachHang is the same");
                Assert.AreEqual(dr["hoKhachHang"], actual.HoKhachHang, "I hope the hoKhachHang is the same");
                Assert.AreEqual(dr["tenLot"], actual.TenLot, "I hope the tenLot is the same");
                Assert.AreEqual(dr["tenKhachHang"], actual.TenKhachHang, "I hope the tenKhachHang is the same");
                Assert.AreEqual((int)dr["maLoaiKH"], actual.MaLoaiKH, "I hope the maLoaiKhachHang is the same");
                Assert.AreEqual(dr["diaChi"], actual.DiaChi, "I hope the diaChi is the same");
                Assert.AreEqual(dr["CMND"], actual.CMND1, "I hope the CMND is the same");
                Assert.AreEqual((int)dr["danhDau"], actual.DanhDau, "I hope the danhDau is the same");
                return true;
            }
            return false;
        }

        [Test]
        public void TestInsert()
        {
            KhachHangDTO kh = new KhachHangDTO();
            kh.HoKhachHang = "Nguyễn";
            kh.TenLot = "Văn";
            kh.TenKhachHang = "B";
            kh.MaLoaiKH = 1;
            kh.DiaChi = "Nguyễn Văn Cừ";
            kh.CMND1 = "032145267";
            kh.DanhDau = 0;

            bool kq = KhachHangDAO.Insert(kh);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("hoKhachHang", kh.HoKhachHang);
            para.Add("tenLot", kh.TenLot);
            para.Add("tenKhachHang", kh.TenKhachHang);
            para.Add("maLoaiKH", kh.MaLoaiKH);
            para.Add("diaChi", kh.DiaChi);
            para.Add("CMND", kh.CMND1);
            para.Add("danhDau", kh.DanhDau);

            expectedData = select_DataTable("KhachHang", para);
            if (expectedData.Rows.Count == 0)
            {
                Assert.IsFalse(kq);
            }
            else
            {
                Assert.GreaterOrEqual(expectedData.Rows.Count, 1, "Cần Insert xuống CSDL");
                Assert.IsTrue(kq);
                Assert.AreEqual(expectedData.Rows[0]["CMND"], kh.CMND1 , "CMND not the same");
            }
            
        }

        [Test]
        public void TestUpdateKhachHang()
        {
            KhachHangDTO kh = new KhachHangDTO();
            kh.MaKhachHang = 1;
            kh.HoKhachHang = "Nguyễn";
            kh.TenLot = "Văn";
            kh.TenKhachHang = "B1";
            kh.MaLoaiKH = 1;
            kh.DiaChi = "Nguyễn Trãi";
            kh.CMND1 = "052149875";
            kh.DanhDau=0;

            bool kq = KhachHangDAO.Update(kh);

            Dictionary<string, object> para = new Dictionary<string, object>();
            para.Add("maKhachHang", kh.MaKhachHang);

            expectedData = select_DataTable("KhachHang", para);
            if (expectedData.Rows.Count == 0)
            {
                Assert.IsFalse(kq);
            }
            else
            {
                Assert_clauses(expectedData, kh);
                Assert.IsTrue(kq);
            }
        }
    }
}
